//
//  DYJingquDetailViewController.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/4.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import GDPerformanceView_Swift

class DYJingquDetailViewController: SYBaseViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
    
    var menpiaoName: String = "门票预约"
    var chizhuName: String = "吃住娱乐"
    var jiangjieName: String = "语音讲解"
    var youlanName: String = "景区游览"
    var zixunName: String = "游客咨询"
//    var jingdianTuijianName: String = "更多景点推荐"
    var headHeight: CGFloat = 0.0
    var menpiaoHeight: CGFloat = 0.0
    var chizhuHeight: CGFloat = 0.0
    var jiangjieHeight: CGFloat = 0.0
    var youlanHeight: CGFloat = 0.0
    var zixunHeight: CGFloat = 0.0
    
    var headRect: CGRect = CGRect.zero
    var menpiaoRect: CGRect = CGRect.zero
    var chizhuRect: CGRect = CGRect.zero
    var jiangjieRect: CGRect = CGRect.zero
    var youlanRect: CGRect = CGRect.zero
    var zixunRect: CGRect = CGRect.zero
    
    var labels: [String] = []
    var tableView: UITableView!
    let navView: SYBaseView = SYBaseView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: NAV_HEIGHT))
    let biaoqian: DYBiaoqianChoiceView = DYBiaoqianChoiceView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: 40))
    let topView: DYDaolanDetailHeadView = DYDaolanDetailHeadView()
    var id: String!
    var model: DYJingquDetailModel!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    
    init(_ id: String) {
        super.init(nibName: nil, bundle: nil)
        self.id = id
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setTableView()
        topMangain()
        creatBiaoqian()
        getData()
    }
    
    public func topMangain() {
        // 解决Webview未覆盖顶部状态栏
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
        } else {
            self.edgesForExtendedLayout = UIRectEdge.bottom
        }
    }
    
    
    func getData() {
        requestVM.baseRequest(disposeBag: dispose, type: .dy_jingquDetail(id: self.id), modelClass: SGBaseModel<DYJingquDetailModel>.self) {[weak self] (res) in
            if let `self` = self {
                self.model = res.data
                
                
                if let m = self.model, m.scenicOrders?.count != 0 {
                    self.labels.append(self.menpiaoName)
                }
                
                if let m = self.model, m.tourHotelVOList?.count != 0 {
                    self.labels.append(self.chizhuName)
                }
                
                if let m = self.model, m.tourScenicExplainVO != nil {
                    self.labels.append(self.jiangjieName)
                }
                
                if let m = self.model, m.tourScenicVisits?.count != 0 {
                    self.labels.append(self.youlanName)
                }
                
                if let m = self.model, m.advisoryList?.count != 0 {
                    self.labels.append(self.zixunName)
                }
                
                
                self.topView.configure(self.model)
                self.tableView.reloadData()
                self.biaoqian.configureView(titles: self.labels)
            }
            
        } Error: {
            
        }
    }
    
    
    func creatBiaoqian() {
        
        navView.isHidden = true
        navView.lineView.isHidden = true
        navView.backgroundColor = .white
        view.addSubview(navView.lineView)
        navView.lineView.frame = CGRect(x: 0, y: NAV_HEIGHT - 0.5, width: SCREEN_WIDTH, height: 0.5)
        let back: UIButton = UIButton()
        back.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        back.setImage(UIImage(named: "back"), for: .normal)
        view.addSubview(navView)
        navView.addSubview(back)
        back.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(STATUSBAR_HEIGHT)
            make.height.equalTo(48.autoWidth())
            make.width.equalTo(30.autoWidth())
        }
        
        biaoqian.isHidden = true
        biaoqian.backgroundColor = .white
        biaoqian.clickedClosure = {[weak self] index in
            if let `self` = self {
                
                var rect: CGRect = CGRect.zero
                
                switch self.labels[index - 100] {
                case self.menpiaoName:
                    rect = self.menpiaoRect
                case self.chizhuName:
                    rect = self.chizhuRect
                case self.jiangjieName:
                    rect = self.jiangjieRect
                case self.youlanName:
                    rect = self.youlanRect
                case self.zixunName:
                    rect = self.zixunRect
                
                default:
                    break
                }
                self.tableView.setContentOffset(CGPoint(x: 0, y: rect.minY - NAV_HEIGHT - 40), animated: true)
            }
        }
        view.addSubview(biaoqian)
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-BOTTOM_SAFE_HEIGHT), style:.grouped)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView!.register(DYYuleCell.self, forCellReuseIdentifier: "DYYuleCell")
        self.tableView!.register(DYYuyinJiangjieCell.self, forCellReuseIdentifier: "DYYuyinJiangjieCell")
        self.tableView!.register(DYYoukeZixunCell.self, forCellReuseIdentifier: "DYYoukeZixunCell")
        self.tableView!.register(DYJingquYoulanCell.self, forCellReuseIdentifier: "DYJingquYoulanCell")
        self.tableView!.register(DYJingdianTuijianCell.self, forCellReuseIdentifier: "DYJingdianTuijianCell")
        self.tableView!.register(DYYueyuCell.self, forCellReuseIdentifier: "DYYueyuCell")
        self.tableView!.bounces = false
        self.tableView.backgroundColor = .white
        self.view.addSubview(self.tableView!)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell: DYYueyuCell? = tableView.dequeueReusableCell(withIdentifier: "DYYueyuCell") as? DYYueyuCell
            cell?.selectionStyle = .none
            cell!.currentVC = self
            if let m = self.model, m.scenicOrders?.count == 0 {
                cell?.isHidden = true
            } else {
                if let m = self.model, let piao = m.scenicOrders?.first {
                    cell!.configureView(piao, indexPath.row, self.model)
                }
            }
            return cell!
        } else if indexPath.row == 1 {
            let cell: DYYuleCell? = tableView.dequeueReusableCell(withIdentifier: "DYYuleCell") as? DYYuleCell
            cell?.selectionStyle = .none
            cell!.currentVC = self
            if let m = self.model, m.tourHotelVOList?.count == 0 {
                cell?.isHidden = true
            } else {
                if let m = self.model {
                    cell!.configureView(m, indexPath.row)
                }
            }
            return cell!
        } else if indexPath.row == 2 {
            let cell: DYYuyinJiangjieCell? = tableView.dequeueReusableCell(withIdentifier: "DYYuyinJiangjieCell") as? DYYuyinJiangjieCell
            cell?.selectionStyle = .none
            cell!.currentVC = self
            if let m = self.model, m.tourScenicExplainVO == nil {
                cell?.isHidden = true
            } else {
                if let m = self.model {
                    cell!.configureView(m)
                }
            }
            return cell!
        }  else if indexPath.row == 3 {
            let cell: DYJingquYoulanCell? = tableView.dequeueReusableCell(withIdentifier: "DYJingquYoulanCell") as? DYJingquYoulanCell
            cell?.selectionStyle = .none
            cell!.currentVC = self
            if let m = self.model, m.tourScenicVisits?.count == 0 {
                cell?.isHidden = true
            } else {
                if let m = self.model {
                    cell!.configureView(m)
                }
            }
            return cell!
        } else if indexPath.row == 4{
            let cell: DYYoukeZixunCell? = tableView.dequeueReusableCell(withIdentifier: "DYYoukeZixunCell") as? DYYoukeZixunCell
            cell?.selectionStyle = .none
            cell!.currentVC = self
            if let m = self.model, m.advisoryList?.count == 0 {
                cell?.isHidden = true
            } else {
                if let m = self.model {
                    
                    cell!.configureView(m.advisoryList ?? [])
                }
            }
            cell?.clickedClosure = {[weak self] _ in
                if let `self` = self {
                    let zixunModel = DYZixunModel()
                    zixunModel.id = self.model.id
                    zixunModel.name = self.model.name
                    
                    var pics: [String] = []
                    if let l = self.model.imgUrlList {
                        
                        for (_, value) in l.enumerated() {
                            if value.fileSuffix == "mp4" {
                                pics.append(value.videoImgUrl ?? "")
                            } else {
                                pics.append(value.fileFullPath ?? "")
                            }
                        }
                    }
                    zixunModel.imgUrl = pics.first
                    
                    let vc = DYZixunViewController(model: zixunModel, listType: .jingqu)
                    self.navigationController?.pushViewController(vc, animated: true)
                }
            }
            return cell!
        } else {
            let cell: DYJingdianTuijianCell? = tableView.dequeueReusableCell(withIdentifier: "DYJingdianTuijianCell") as? DYJingdianTuijianCell
            cell?.selectionStyle = .none
            cell!.currentVC = self
            if let m = self.model, m.scenicSpotList?.count == 0 {
                cell?.isHidden = true
            } else {
                if let m = self.model {
                    cell!.configureView(m)
                }
            }
            
            return cell!
            
        }
        
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        topView.currentVC = self
        return topView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 436.5.autoWidth() + STATUSBAR_HEIGHT
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 0.01))
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        switch indexPath.row {
        case 0:
            if let m = self.model, m.scenicOrders?.count == 0 {
                return 0.0
            }
        case 1:
            if let m = self.model, m.tourHotelVOList?.count == 0 {
                return 0.0
            }
        case 2:
            if let m = self.model, m.tourScenicExplainVO == nil {
                return 0.0
            }
        case 3:
            if let m = self.model, m.tourScenicVisits?.count == 0 {
                return 0.0
            }
        case 4:
            if let m = self.model, m.advisoryList?.count == 0 {
                return 0.0
            }
        case 5:
            if let m = self.model, m.scenicSpotList?.count == 0 {
                return 0.0
            }
        default:
            break
        }
        
        return UITableView.automaticDimension
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let y = scrollView.contentOffset.y + 40 + NAV_HEIGHT
        
        headHeight = self.tableView.rectForHeader(inSection: 0).maxY
        
        menpiaoRect = self.tableView.rectForRow(at: IndexPath(row: 0, section: 0))
        chizhuRect = self.tableView.rectForRow(at: IndexPath(row: 1, section: 0))
        jiangjieRect = self.tableView.rectForRow(at: IndexPath(row: 2, section: 0))
        youlanRect = self.tableView.rectForRow(at: IndexPath(row: 3, section: 0))
        zixunRect = self.tableView.rectForRow(at: IndexPath(row: 4, section: 0))
        
        menpiaoHeight = self.menpiaoRect.maxY
        chizhuHeight = self.chizhuRect.maxY
        jiangjieHeight = self.jiangjieRect.maxY
        youlanHeight = self.youlanRect.maxY
        zixunHeight = self.zixunRect.maxY
//        print("y: \(y)  headHeight:\(headHeight)  menpiaoHeight:\(menpiaoHeight)  chizhuHeight:\(chizhuHeight)  jiangjieHeight:\(jiangjieHeight)  youlanHeight:\(youlanHeight)  zixunHeight:\(zixunHeight)")
        
        if y >= headHeight {
            biaoqian.isHidden = false
            navView.isHidden = false
            navView.lineView.isHidden = false
        } else {
            biaoqian.isHidden = true
            navView.isHidden = true
            navView.lineView.isHidden = true
        }
        
        
        if y >= headHeight && y < menpiaoHeight {
            biaoqian.refreshItems(self.menpiaoName)
        } else if y >= menpiaoHeight && y < chizhuHeight {
            biaoqian.refreshItems(self.chizhuName)
        } else if y >= chizhuHeight && y < jiangjieHeight {
            biaoqian.refreshItems(self.jiangjieName)
        } else if y >= jiangjieHeight && y < youlanHeight {
            biaoqian.refreshItems(self.youlanName)
        } else if y >= youlanHeight && y < zixunHeight {
            biaoqian.refreshItems(self.zixunName)
        } else if y >= zixunHeight {
//            biaoqian.refreshItems(self.jingdianTuijianName)
        }
        
    }
    
    @objc func backAction() {
        self.navigationController?.popViewController(animated: true)
    }
}
